package journal

import (
	"fmt"
	"go.uber.org/zap"
	"go.uber.org/zap/zapcore"
	"gopkg.in/natefinch/lumberjack.v2"
	"runtime"
)

var path string

func (cfg ZapConfig) ZapCustom(level string) {
	SetLogger(cfg.PrefixName+level, NewLogger(cfg.PrefixName+"_"+level+".log"))
}

func (cfg ZapConfig) ZapSimpleLog(sign string, keysAndValues ...interface{}) {
	_, file, line, _ := runtime.Caller(1)
	keysAndValues = append(keysAndValues, file)
	keysAndValues = append(keysAndValues, line)
	for k, v := range keysAndValues {
		keysAndValues[k] = fmt.Sprintf("%v", v)
	}
	GetLogger(cfg.PrefixName+"Info").Infow(sign, keysAndValues...)
}

func (cfg ZapConfig) ZapErrorLog(sign string, keysAndValues ...interface{}) {
	_, file, line, _ := runtime.Caller(1)
	keysAndValues = append(keysAndValues, file)
	keysAndValues = append(keysAndValues, line)
	for k, v := range keysAndValues {
		keysAndValues[k] = fmt.Sprintf("%v", v)
	}
	GetLogger(cfg.PrefixName+"Error").Infow(sign, keysAndValues...)
}

func (cfg ZapConfig) ZapPanicLog(sign string, keysAndValues ...interface{}) {
	_, file, line, _ := runtime.Caller(1)
	keysAndValues = append(keysAndValues, file)
	keysAndValues = append(keysAndValues, line)
	for k, v := range keysAndValues {
		keysAndValues[k] = fmt.Sprintf("%v", v)
	}
	GetLogger(cfg.PrefixName+"Panic").Infow(sign, keysAndValues...)
}

func (cfg ZapConfig) ZapCustomLog(level string, sign string, keysAndValues ...interface{}) {
	_, file, line, _ := runtime.Caller(1)
	keysAndValues = append(keysAndValues, file)
	keysAndValues = append(keysAndValues, line)
	for k, v := range keysAndValues {
		keysAndValues[k] = fmt.Sprintf("%v", v)
	}
	GetLogger(cfg.PrefixName+level).Infow(sign, keysAndValues...)
}

func (cfg ZapConfig) InitLog() {
	path = cfg.Path
	SetLogger(cfg.PrefixName+"Info", NewLogger(cfg.PrefixName+"_info.log"))
	SetLogger(cfg.PrefixName+"Error", NewLogger(cfg.PrefixName+"_err.log"))
	SetLogger(cfg.PrefixName+"Panic", NewLogger(cfg.PrefixName+"_panic.log"))
}

var maploggers map[string]*zap.SugaredLogger

func init() {
	maploggers = make(map[string]*zap.SugaredLogger)
}

func GetLogger(logName string) *zap.SugaredLogger {
	if v, ok := maploggers[logName]; ok {
		return v
	}
	return nil
}

func SetLogger(logName string, logger *zap.SugaredLogger) {
	maploggers[logName] = logger
}

func NewLogger(fileName string) *zap.SugaredLogger {

	if tmp := GetLogger(fileName); tmp != nil {
		return tmp
	}

	fileName = path + fileName
	fmt.Println("log path:", fileName)

	w := zapcore.AddSync(&lumberjack.Logger{
		Filename:   fileName,
		MaxSize:    20,   // megabytes
		MaxBackups: 20,   // 最多保留20个备份
		MaxAge:     7,    // days
		Compress:   true, // 是否压缩备份文件
	})

	newDevelopmentEncoderConfig := zap.NewDevelopmentEncoderConfig()
	newDevelopmentEncoderConfig.TimeKey = "T"
	newDevelopmentEncoderConfig.CallerKey = "T"
	newDevelopmentEncoderConfig.EncodeTime = zapcore.ISO8601TimeEncoder

	core := zapcore.NewTee(
		zapcore.NewCore(zapcore.NewJSONEncoder(newDevelopmentEncoderConfig), w, zapcore.InfoLevel),
	)
	logger := zap.New(core)
	return logger.Sugar()
}
